package zym.diandian.module.zan;


import java.util.List;

import android.accessibilityservice.AccessibilityService;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.SystemClock;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.Toast;

public class ZanService extends AccessibilityService {

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        // TODO Auto-generated method stub
        int type = event.getEventType();


        System.out.println(" T YPE " + type);

        switch (type) {
            case AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED:
                System.out.println("TYPE_VIEW_TEXT_CHANGED");
                break;
            case AccessibilityEvent.TYPE_VIEW_CLICKED:
                System.out.println("TYPE_VIEW_CLICKED");
                break;
            case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED:
                System.out.println("TYPE_WINDOW_CONTENT_CHANGED");
                break;
            case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
                System.out.println("TYPE_WINDOW_STATE_CHANGED");
                System.out.println("WindowsId:" + event.getWindowId());
                System.out.println("ClassName:" + event.getClassName());
                System.out.println("PackageName:" + event.getPackageName());

                String className = event.getClassName().toString();

                if (className.contains("FriendProfileCardActivity")) {
                    SystemClock.sleep(1000);
                    // 好友资料页
                    AccessibilityNodeInfo info = getRootInActiveWindow();

                    if (info != null) {
                        List<AccessibilityNodeInfo> infos = info.findAccessibilityNodeInfosByText("点击可赞");
                        if (infos == null || infos.size() < 1) {
                            Toast.makeText(getApplicationContext(), "当前用户不可点赞", Toast.LENGTH_SHORT).show();
                        } else {
                            for (int i = 0; i < 10; i++) {
                                AccessibilityNodeInfo childInfo = infos.get(0);
                                System.out.println("描述:" + childInfo.getContentDescription());
                                if (childInfo != null && childInfo.isClickable()) {
                                    childInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                                }
                            }
                        }
                    }
                } else if (className.contains("NearbyPeopleProfileActivity")) {
                    SystemClock.sleep(1000);
                    // 非好友资料页(附近人)
                    AccessibilityNodeInfo info = getRootInActiveWindow();

                    if (info != null) {
                        List<AccessibilityNodeInfo> infos = info.findAccessibilityNodeInfosByText("已赞过");
                        if (infos == null || infos.size() < 1) {
                            Toast.makeText(getApplicationContext(), "当前用户不可点赞", Toast.LENGTH_SHORT).show();
                        } else {
                            for (int i = 0; i < 10; i++) {
                                AccessibilityNodeInfo childInfo = infos.get(0);
                                System.out.println("描述:" + childInfo.getContentDescription());
                                if (childInfo != null && childInfo.isClickable()) {
                                    childInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                                }
                            }
                        }
                    }
                }
                break;
            case AccessibilityEvent.TYPE_WINDOWS_CHANGED:
                System.out.println("TYPE_WINDOWS_CHANGED");
                break;
            default:
                break;
        }
    }


    private void back() {
        this.performGlobalAction(GLOBAL_ACTION_BACK);
    }

    @Override
    public void onInterrupt() {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "onInterrupt", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onServiceConnected() {
        // TODO Auto-generated method stub
        super.onServiceConnected();
        Toast.makeText(getApplicationContext(), "onServiceConnected", Toast.LENGTH_SHORT).show();
        System.out.println("onServiceConnected");
    }

    @Override
    public boolean onUnbind(Intent intent) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "onUnbind", Toast.LENGTH_SHORT).show();
        System.out.println("onUnbind");
        return super.onUnbind(intent);
    }
}
